package cn.com.chb.dakele.util;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.com.chb.dakele.net.Http;
import cn.com.chb.dakele.net.HttpGetHead;

public class UrlFetch {
	public static void main(String[] args) {
		String url = "http://www.qiushibaike.com";
		String host = Http.getHost(url);
		System.out.println(host);
		HttpGetHead get = new HttpGetHead(url);
		CloseableHttpResponse response;
		try {
			response = Http.getClient().execute(get);
			System.out.println(get.getURI().getHost());
			HttpEntity entity = response.getEntity();
			String html = EntityUtils.toString(entity);
			/*
			Document document = Jsoup.parse(html,Http.getHost(url));
			Elements divElements = document.select("a");
			for(Element a : divElements){
					if(a.attr("target").equals("_blank")){
						
						if(!Http.getHost(a.attr("abs:href")).equals(host)){
							System.out.println((a.attr("abs:href").trim()));
						}
					
				}
			}
			*/
			Document document2 = Jsoup.parse(html,Http.getHost(url));
			Elements content = document2.select("img");
			for(Element c:content){
				System.out.println(c);
				System.out.println("------------------------------------");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
